package cn.ronghuanet.auth.service.impl;

import cn.ronghuanet.auth.service.ILoginService;
import cn.ronghuanet.auth.vo.LoginVO;
import cn.ronghuanet.base.context.LoginContext;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IEmployeeService employeeService;
    @Override
    public LoginVO login(Employee employee) {
        // 数据判空
        if (Objects.isNull(employee.getUsername())
                || Objects.isNull(employee.getPassword())
        ) throw new RuntimeException("账号或密码不正确");
        // 根据账户查对象
        Employee user = employeeService.queryByName(employee);
        if (Objects.isNull(user)) throw new RuntimeException("账号或密码不正确");
        // 判断密码是否正确
        if(!user.getPassword().equals(employee.getPassword()))throw new RuntimeException("账号或密码不正确");
        // 生成UUID返回
        String token = UUID.randomUUID().toString();
        // 将用户信息存全局map  以后存redis
        LoginContext.loginMap.put(token,user);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(token);
        // 这个user是不是要返给前端? 有没有什么不妥的地方
        // 要进行数据脱敏
        user.setPassword(null);
//        user.setId(null);
        user.setEmail(null);
        loginVO.setUser(user);
        return loginVO;
    }
}
